package org.misalen.web.admin.jstltag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.misalen.web.admin.shiro.ShiroUser;
import org.misalen.web.database.model.mysql.sys.SysWordbook;
import org.misalen.web.database.model.mysql.sys.SysWordbookItem;
import org.misalen.web.database.repository.mysql.SysWordbookItemRepository;
import org.misalen.web.database.repository.mysql.SysWordbookRepository;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

public class WordbookHtml extends TagSupport {

	protected SysWordbookItemRepository sysWordbookItemRepository;
	protected SysWordbookRepository sysWordbookRepository;
	protected WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();

	public WordbookHtml() {
		sysWordbookItemRepository = wac.getBean(SysWordbookItemRepository.class);
		sysWordbookRepository = wac.getBean(SysWordbookRepository.class);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = this.pageContext.getOut();
		try {
			SysWordbook sysWordbook = sysWordbookRepository.getByCode(wbCode);
			List<SysWordbookItem> items = null;
			if(wbCode.equals("pro-city")) {
				ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
				String belongCity = user.getBelongCity();
				if(!belongCity.equals("全国")) {
					items = sysWordbookItemRepository.findByWbIdAndName(sysWordbook.getPrimaryKey(), belongCity);
				}else {
					items = sysWordbookItemRepository.findByWbId(sysWordbook.getPrimaryKey());
				}
			}else {
				items = sysWordbookItemRepository.findByWbId(sysWordbook.getPrimaryKey());
			}
			if (StringUtils.isEmpty(name)) {
				name = wbCode;
			}
			if (type.equals("select")) {
				StringBuffer buffer = new StringBuffer();
				buffer.append("<select name=\"");
				buffer.append(name);
				buffer.append("\" class=\"form-control\">");
				if (nullvalue != null && nullvalue) {
					buffer.append("<option value=\"");
					buffer.append("\">");
					buffer.append("</option>");
				}
				for (SysWordbookItem sysWordbookItem : items) {
					buffer.append("<option value=\"");
					buffer.append(sysWordbookItem.getItemCode());
					buffer.append("\">");
					buffer.append(sysWordbookItem.getItemName());
					buffer.append("</option>");
				}
				buffer.append("</select>");
				out.print(buffer.toString());
			} else {
				JSONObject jsonObject = new JSONObject();
				for (SysWordbookItem sysWordbookItem : items) {
					jsonObject.put(sysWordbookItem.getItemCode(), sysWordbookItem.getItemName());
				}
				out.print(jsonObject.toString());
			}

		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return super.doStartTag();
	}

	@Override
	public void release() {
		wbCode = null;
		type = null;
		name = null;
		value = null;
		super.release();
	}

	private String wbCode;
	private String type;
	private String name;
	private String value;
	private Boolean nullvalue;

	public Boolean getNullvalue() {
		return nullvalue;
	}

	public void setNullvalue(Boolean nullvalue) {
		this.nullvalue = nullvalue;
	}

	public String getWbCode() {
		return wbCode;
	}

	public void setWbCode(String wbCode) {
		this.wbCode = wbCode;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

}
